import re
import time
import logging
from datetime import datetime, timedelta

def setup_logging():
    """配置日志"""
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(levelname)s - %(message)s',
        handlers=[
            logging.FileHandler('weibo_crawler.log', encoding='utf-8'),
            logging.StreamHandler()
        ]
    )

def extract_media_urls(html_content):
    """从HTML内容中提取媒体URL"""
    img_pattern = r'https?://[^\s<>"]+?(?:jpg|jpeg|png|gif)'
    video_pattern = r'https?://[^\s<>"]+?(?:mp4|avi|mov)'
    
    images = ','.join(re.findall(img_pattern, html_content))
    videos = ','.join(re.findall(video_pattern, html_content))
    
    return images or videos or None

def parse_time(time_str):
    """解析微博时间字符串"""
    now = datetime.now()
    
    if '分钟前' in time_str:
        minutes = int(re.search(r'\d+', time_str).group())
        return now - timedelta(minutes=minutes)
    elif '小时前' in time_str:
        hours = int(re.search(r'\d+', time_str).group())
        return now - timedelta(hours=hours)
    elif '昨天' in time_str:
        return now - timedelta(days=1)
    elif '月' in time_str and '日' in time_str:
        month, day = map(int, re.findall(r'\d+', time_str))
        return datetime(now.year, month, day)
    else:
        try:
            return datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
        except:
            return now

def extract_location(text):
    """从文本中提取位置信息"""
    location_pattern = r'【位置】(.*?)(?:【|$)'
    match = re.search(location_pattern, text)
    if match:
        return match.group(1).strip()
    return None

def extract_topics(text):
    """提取微博话题"""
    topic_pattern = r'#(.*?)#'
    return re.findall(topic_pattern, text)

def retry_on_failure(max_retries=3, delay=5):
    """失败重试装饰器"""
    def decorator(func):
        def wrapper(*args, **kwargs):
            for i in range(max_retries):
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    if i == max_retries - 1:
                        logging.error(f"重试{max_retries}次后仍然失败: {str(e)}")
                        raise
                    logging.warning(f"第{i+1}次尝试失败，{delay}秒后重试: {str(e)}")
                    time.sleep(delay)
        return wrapper
    return decorator

def clean_text(text):
    """清理文本内容"""
    # 移除HTML标签
    text = re.sub(r'<[^>]+>', '', text)
    # 移除多余空白字符
    text = re.sub(r'\s+', ' ', text)
    # 移除表情符号
    text = re.sub(r'[\U0001F600-\U0001F64F\U0001F300-\U0001F5FF\U0001F680-\U0001F6FF\U0001F1E0-\U0001F1FF]', '', text)
    return text.strip()

def extract_user_info(html_content):
    """从HTML中提取用户信息"""
    username_pattern = r'nick-name="(.*?)"'
    avatar_pattern = r'avatar="(.*?)"'
    
    username = re.search(username_pattern, html_content)
    avatar = re.search(avatar_pattern, html_content)
    
    return {
        'username': username.group(1) if username else None,
        'avatar': avatar.group(1) if avatar else None
    }

def calculate_hot_score(likes, comments, reposts, time_delta):
    """计算热度分数"""
    # 时间衰减因子（24小时内的内容权重较高）
    time_factor = 1.0 / (1 + time_delta.total_seconds() / (24 * 3600))
    
    # 互动权重
    like_weight = 1
    comment_weight = 2
    repost_weight = 3
    
    # 计算总分
    score = (likes * like_weight + 
             comments * comment_weight + 
             reposts * repost_weight) * time_factor
    
    return score 